package com.yitiao.data_algorithms.leetcode.editor.cn;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class FindTargetIndicesAfterSortingArray {
    // 2023-04-03 17:07:58
    public static void main(String[] args) {
        Solution solution = new FindTargetIndicesAfterSortingArray().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public List<Integer> targetIndices(int[] nums, int target) {
            Arrays.sort(nums);
            List<Integer> ans = new ArrayList<>();
            int l = 0;
            int r = nums.length - 1;
            while (l <= r) {
                int mid = l + (r - l) / 2;
                if (nums[mid] == target) {
                    ans.add(mid);
                    int temp = mid;
                    while (mid <= nums.length - 2 && nums[++mid] == target) {
                        ans.add(mid);
                    }
                    while (temp >= 1 && nums[--temp] == target) {
                        ans.add(temp);
                    }
                    Collections.sort(ans);
                    return ans;
                } else if (nums[mid] > target) {
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            }
            return ans;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}